﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for WidthHeightControl.xaml
    /// </summary>
    public partial class WidthHeightControl : UserControl
    {
        public event EventHandler<ObjectEventArgsNonRef<double, double>> ChangeSize;
        public double _lastWidth;
        public double _lastHeight;

        public WidthHeightControl()
        {
            InitializeComponent();            
        }

        public void SetLastWidthHeight(int width, int height)
        {
            _lastWidth = (double)width;
            _lastHeight = (double)height;
            txtWidth.Text = width.ToString();
            txtHeight.Text = height.ToString();
        }

        public void SetWidthHeight()
        {
            double width;
            double height;

            if (!double.TryParse(txtWidth.Text, out width))
            {                
                txtWidth.Focus();
                return;
            }

            if (!double.TryParse(txtHeight.Text, out height))
            {             
                txtHeight.Focus();
                return;
            }

            if (ChangeSize != null)
                ChangeSize(this, new ObjectEventArgsNonRef<double, double>(width, height));
        }
        
        private void txtWidth_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetWidthHeight();
            }
        }

        private void txtHeight_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetWidthHeight();
            }
        }

        private void btnChange_Click(object sender, RoutedEventArgs e)
        {
            SetWidthHeight();
        }
    }
}
